package com.worldcup.controller.relationship;

import com.worldcup.common.CommonResult;
import com.worldcup.model.User;
import com.worldcup.model.dto.question.MatchResultSurveyDTO;
import com.worldcup.model.question.QuestionMatchResult;
import com.worldcup.model.question.UserRelationshipEvent;
import com.worldcup.service.UserService;
import com.worldcup.service.question.QuestionMatchResultService;
import com.worldcup.service.relation.UserRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户关系Controller
 * @author duwan.zq
 * @date 2019/03/22
 */
@RestController
@RequestMapping("/api/java/user/relation/")
public class UserRelationController {

    @Autowired
    private UserRelationService userRelationService;

    @Autowired
    private UserService userService;

    @Autowired
    private QuestionMatchResultService matchResultService;

    @RequestMapping("/event")
    public CommonResult relationshipEvent(@RequestBody UserRelationshipEvent relationshipEvent){
        userRelationService.insert(relationshipEvent);
        return CommonResult.buildSuccess();
    }

    /**
     * 我想认识的人
     * @return
     */
    @RequestMapping("/list/know/want")
    public CommonResult wantKnow(String openid){
        List<MatchResultSurveyDTO> result = new ArrayList();

        List<String> friendOpenids = userRelationService.getFriendIds(openid);
        for(String friendOpenid : friendOpenids){
            User user = userService.getUser(friendOpenid);
            //获取匹配结果
            QuestionMatchResult matchResult = matchResultService.getMatchResult(openid, friendOpenid);
            result.add(new MatchResultSurveyDTO(user, matchResult));
        }
        return CommonResult.buildSuccess(result);
    }

    /**
     * 想认识我的人
     * @return
     */
    @RequestMapping("/list/know/wanted")
    public CommonResult wantedKnow(String openid){
        List<MatchResultSurveyDTO> result = new ArrayList();

        List<String> friendOpenids = userRelationService.getOpenids(openid);
        for(String friendOpenid : friendOpenids){
            User user = userService.getUser(friendOpenid);
            //获取匹配结果
            QuestionMatchResult matchResult = matchResultService.getMatchResult(openid, friendOpenid);
            result.add(new MatchResultSurveyDTO(user, matchResult));
        }
        return CommonResult.buildSuccess(result);
    }

    /** 精准推荐 即非好友的排名 **/
    public CommonResult recommend(String openid){
        List<MatchResultSurveyDTO> result = new ArrayList();

        List<String> friendOpenids = userRelationService.getOpenids(openid);
        List<QuestionMatchResult> matchResults = matchResultService.getMatchResultExclude(openid, friendOpenids);
        for(QuestionMatchResult matchResult : matchResults){
            User user = userService.getUser(matchResult.getPassiveOpenid());
            result.add(new MatchResultSurveyDTO(user, matchResult));
        }
        return CommonResult.buildSuccess(result);

    }


    /**
     * 可能感兴趣的人
     */
    public CommonResult randomInterest(String openid){
        matchResultService.randomInterest(openid);
        //todo
        return null;
    }



}
